package org.feifei.pet.service.impl;

import org.feifei.basic.service.impl.BaseServiceImpl;
import org.feifei.basic.util.LoginContextUtil;
import org.feifei.org.domain.Shop;
import org.feifei.org.mapper.ShopMapper;
import org.feifei.pet.domain.Pet;
import org.feifei.pet.domain.PetDetail;
import org.feifei.pet.mapper.PetDetailMapper;
import org.feifei.pet.mapper.PetMapper;
import org.feifei.pet.service.PetService;
import org.feifei.user.domain.LoginInfo;
import org.feifei.user.domain.User;
import org.feifei.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements PetService {

    @Resource
    private PetMapper petMapper;
    @Resource
    private PetDetailMapper petDetailMapper;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public void editORSave(Pet pet) {
        PetDetail detail = pet.getDetail();
        pet.setType_id(pet.getType().getId());
        if (pet.getId() == null) {
            //添加宠物信息
            super.save(pet);
            //添加明细
            if(detail != null){
                detail.setPet_id(pet.getId());
                petDetailMapper.save(detail);
            }
        } else {
            if (detail != null) {
                petDetailMapper.update(detail);
            }
            petMapper.update(pet);
        }

    }

    @Override
    public void onsale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("onsaleTime",new Date());
        map.put("ids", ids);
        petMapper.onsale(map);
    }

    @Override
    public void offsale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("offsaleTime",new Date());
        map.put("ids", ids);
        petMapper.offSale(map);
    }

    @Override
    public void adopt(Long id, HttpServletRequest request) {
        Pet pet = petMapper.get(id);
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        User user = userMapper.getByLoginId(loginInfo.getId());
        pet.setUser_id(user.getId()).setOffsaletime(new Date()).setState(0);
        petMapper.update(pet);
    }

    @Override
    public void remove(Long id) {
        petDetailMapper.removeByPetId(id);
        super.remove(id);
    }

    @Override
    public void batchRemove(List<Long> ids) {
        petDetailMapper.batchRemoveByPetIds(ids);
        super.batchRemove(ids);
    }

    @Override
    public void save(Pet pet) {
        PetDetail detail = pet.getDetail();
        //添加宠物信息
        super.save(pet);
        //添加明细
        if(detail != null){
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }
    }

    @Override
    public Pet get(Long id) {

        Pet pet = super.get(id);
        PetDetail petDetail = petDetailMapper.getByPetId(id);
        if (pet.getShop_id() != null) {
            pet.setShop(shopMapper.get(pet.getShop_id()));
        }
        pet.setDetail(petDetail);
        return pet;
    }
}
